package com.web.controller.cargo;

import com.alibaba.dubbo.config.annotation.Reference;
import com.domain.cargo.*;
import com.domain.vo.ExportProductVo;
import com.domain.vo.ExportResult;
import com.domain.vo.ExportVo;
import com.github.pagehelper.PageInfo;
import com.service.cargo.ContractService;
import com.service.cargo.ExportProductService;
import com.service.cargo.ExportService;
import com.web.controller.BaseController;
import common.utils.BeanMapUtils;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
import org.apache.commons.lang3.StringUtils;
import org.apache.cxf.jaxrs.client.WebClient;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/cargo/export")
public class ExportController extends BaseController {

    @Reference
    private ExportService exportService;
    @Reference
    private ExportProductService exportProductService;

    @Reference
    private ContractService contractService;

    @RequestMapping("/contractList")
    public String contractList(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "5") Integer size){
        ContractExample contractExample = new ContractExample();
        contractExample.createCriteria().andCompanyIdEqualTo(companyId).andStateEqualTo(1);
        PageInfo pageInfo = contractService.findAll(contractExample, page, size);
        request.setAttribute("page", pageInfo);
        return "/cargo/export/export-contractList";
    }

    @RequestMapping("/list")
    public String list(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "5") Integer size){
        ExportExample exportExample = new ExportExample();
        exportExample.createCriteria().andCompanyIdEqualTo(companyId);
        PageInfo pageInfo = exportService.findAll(exportExample, page, size);
        request.setAttribute("page", pageInfo);
        return "/cargo/export/export-list";
    }

    @RequestMapping("/toUpdate")
    public String toUpdate(String id){
        Export export = exportService.findById(id);
        request.setAttribute("export", export);

        //因为前端还需要展示报运货物,获取一下
        ExportProductExample exportProductExample = new ExportProductExample();
        exportProductExample.createCriteria().andExportIdEqualTo(id);
        List<ExportProduct> exportProducts = exportProductService.findAll(exportProductExample);
        request.setAttribute("eps", exportProducts);

        return "/cargo/export/export-update";
    }

    @RequestMapping("/toView")
    public String toView(String id){
        Export export = exportService.findById(id);
        request.setAttribute("export", export);
        return "/cargo/export/export-view";
    }

    @RequestMapping("/edit")
    public String edit(Export export){
        export.setCompanyId(companyId);
        export.setCompanyName(companyName);

        if (StringUtils.isBlank(export.getId())) {
            exportService.save(export);
        }else{
            exportService.update(export);
        }
        return "redirect:/cargo/export/list.do";
    }

    @RequestMapping("/delete")
    public String delete(String id){
        exportService.delete(id);
        return "redirect:/cargo/export/list.do";
    }

    @RequestMapping("/toExport")
    public String toExport(String id){
        request.setAttribute("id", id);
        return "cargo/export/export-toExport";
    }

    @RequestMapping("/submit")
    public String submit(String id){
        Export export = exportService.findById(id);
        export.setState(1);
        exportService.update(export);
        return "redirect:/cargo/export/list.do";
    }

    @RequestMapping("/cancel")
    public String cancel(String id){
        Export export = exportService.findById(id);
        export.setState(0);
        exportService.update(export);
        return "redirect:/cargo/export/list.do";
    }

    @RequestMapping("/exportE")
    //电子报运,需要访问海关的WebService,海关规定了请求的数据格式,所以要进行处理
    public String exportE(String id){
        /**
         * 第一次请求:调用海关报运接口(第一次调用)post方法
         */
        //电子报运单实体类
        ExportVo exportVo = new ExportVo();
        //new一个ArrayList,存储所有的电子报运货物
        List<ExportProductVo> list = new ArrayList<>();

        //通过id查询报运单实体类
        Export export = exportService.findById(id);
        //复制到电子报运单
        BeanUtils.copyProperties(export, exportVo);
        //设置报运单id
        exportVo.setExportId(id);

        //通过id查询所有的报运货物
        ExportProductExample exportProductExample = new ExportProductExample();
        exportProductExample.createCriteria().andExportIdEqualTo(id);
        List<ExportProduct> exportProducts = exportProductService.findAll(exportProductExample);

        //遍历,生成对应的电子报运货物,并添加到list
        for (ExportProduct exportProduct : exportProducts) {
            ExportProductVo exportProductVo = new ExportProductVo();
            BeanUtils.copyProperties(exportProduct, exportProductVo);
            exportProductVo.setExportProductId(exportProduct.getId());
            list.add(exportProductVo);
        }
        //将list赋值到ExportVo实体类的products属性
        exportVo.setProducts(list);

        //请求地址
        WebClient webClient = WebClient.create("http://localhost:8888/ws/export/user");
        //传递数据:电子报运单实体类
        webClient.post(exportVo);

        /**
         * 第二次请求:调用海关报运接口(第二次调用)get方法,携带id查询
         */
        WebClient webClient2 = WebClient.create("http://localhost:8888/ws/export/user/" + id);
        //参数为ExportResult.class类型
        ExportResult exportResult = webClient2.get(ExportResult.class);

        //存储到数据库
        exportService.updateE(exportResult);
        return "redirect:/cargo/export/list.do";
    }

    @RequestMapping("/exportPdf")
    //生成pdf文档,在网页显示或下载,不需要返回值
    public void exportPdf(String id) throws JRException, IOException {
        //获取jasper文件
        System.out.println(session.getServletContext().getRealPath("/"));
        String path = session.getServletContext().getRealPath("/") + "jasper/export.jasper";

        //需要出口报运单的pdf文档,需要map和list格式
        //通过id查询报运单实体类,转为Map
        Export export = exportService.findById(id);
        Map<String, Object> exportMap = BeanMapUtils.beanToMap(export);

        //查询对应的所有货物
        ExportProductExample exportProductExample = new ExportProductExample();
        exportProductExample.createCriteria().andExportIdEqualTo(id);
        List<ExportProduct> eProductList = exportProductService.findAll(exportProductExample);
        //list进行pdf填充,需要通过JRBeanCollectionDataSource
        JRBeanCollectionDataSource collectionDataSource = new JRBeanCollectionDataSource(eProductList);

        //fillReport(路径, map, list)
        JasperPrint jasperPrint = JasperFillManager.fillReport(path, exportMap, collectionDataSource);
        //通过流响应到页面
        JasperExportManager.exportReportToPdfStream(jasperPrint, response.getOutputStream());

    }

}
